using System;
using System.Collections.Generic;
using System.Text;

namespace MoGameLib
{
    public class StringIdObjectCollection<T> : ICollection<T> where T : IStringIdentified, new()
    {
        #region ICollection<T> Members

        protected Dictionary<string, T> m_Objects = new Dictionary<string, T>();
        
        public void Add(T item)
        {
            // TODO: assert if char is already there
            m_Objects.Add(item.StringId, item);
        }

        public void Clear()
        {
            m_Objects.Clear();
        }

        public bool Contains(T item)
        {
            return m_Objects.ContainsValue(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            m_Objects.Values.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return m_Objects.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            return m_Objects.Remove(item.StringId);
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return m_Objects.Values.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        public T this[string id]
        {
            get { return m_Objects[id]; }
        }

        public T Create(string id)
        {
            T newObject = new T();
            newObject.StringId = id;
            Add(newObject);
            return newObject;
        }
    }
}
